<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 16-6-5
 * Time: 下午8:27
 */
namespace file;
class file{
    private $_dir;
    const FILE_TYPE='sdasdasdadsa';
    public function __construct(){
        $this->_dir=dirname(__FILE__).'/files/';
        if(!is_dir($this->_dir)){
            mkdir($this->_dir,0777);
        }
    }
    public function cacheData($key,$value='',$path=''){
        !empty($path) ? $this->_dir.=$path.'/' : $path ;
        echo $file_name=$this->_dir.$key.'.'.self::FILE_TYPE;

        if($value!==''){
            if($value===null){
               return unlink($file_name);
            }
            if(!is_dir($this->_dir)){
                mkdir($this->_dir,0777);

            }
//            if(!empty($value)){
//                $res=file_get_contents($file_name);
//                return json_decode($res);
//            }
            if(!is_file($file_name)){
                if(file_put_contents($file_name,json_encode($value))>0){
                    return 'success';
                }else{
                    return 'error';
                }
            }
        }
        if($value===''){
            if(!is_file($file_name)){
                return '文件不存在';
            }else{
                return json_decode(file_get_contents($file_name));
            }
        }


    }

}